package com.building.hcrm.wcf;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.R.integer;
import android.content.Context;
import android.util.Log;

import com.building.hcrm.base.BaseServiceProxy;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.ComFunc;
import com.building.hcrm.common.DateDeserializer;
import com.building.hcrm.common.HttpHelper;
import com.building.hcrm.model.Attribution;
import com.building.hcrm.model.AttributionExamine;
import com.building.hcrm.model.AttributionQueryParam;
import com.building.hcrm.model.CallLog;
import com.building.hcrm.model.CustomerAttribution;
import com.building.hcrm.model.CustomerDetail;
import com.building.hcrm.model.CustomerExamine;
import com.building.hcrm.model.CustomerQuery;
import com.building.hcrm.model.CustomerQueryByPhoneParam;
import com.building.hcrm.model.ExamineQuery;
import com.building.hcrm.model.SMSLog;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class CustomerServiceProxy extends BaseServiceProxy {

	private static CustomerServiceProxy _instance;

	public static CustomerServiceProxy getInstance(Context context) {
		if (_instance == null) {
			_instance = new CustomerServiceProxy();
		}
		_instance._context = context;
		return _instance;
	}

	/**
	 * 保存客户信息
	 * 
	 * @param customerDetail
	 * @return
	 * @throws Exception
	 */
	public String CustomerCreate(CustomerDetail customerDetail)
			throws Exception {
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(customerDetail);
		strPost = "{\"model\":" + strPost + "}";
		String strReturn = HttpHelper.getInstance(_context).HttpPost(
				ComConst.SERVICE_URL, "CustomerCreate", strPost);
		if (ValidateResult(strReturn)) {
			JSONObject jsonObject = new JSONObject(strReturn);
			strReturn = jsonObject.getString("CustomerCreateResult");
		}
		return strReturn;
	}

	/**
	 * 根据电话号码获取客户信息
	 * 
	 * @param customerQueryByPhone
	 * @return
	 * @throws Exception
	 */
	public CustomerQuery CustomerDetailByPhone(
			CustomerQueryByPhoneParam customerQueryByPhone) throws Exception {
		CustomerQuery customerQuery = null;
		GsonBuilder gBuilder = new GsonBuilder();
		DateDeserializer dd = new DateDeserializer();
		gBuilder.registerTypeAdapter(Date.class, dd);
		Gson gson = gBuilder.create();
		String strPost = gson.toJson(customerQueryByPhone);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(
				ComConst.SERVICE_URL, "CustomerDetailByPhone", strPost);
		if (ValidateResult(strReturn)) {
			List<String> lstRe = ProcessResult(strReturn,
					"CustomerDetailByPhoneResult");
			for (String str : lstRe) {
				customerQuery = new CustomerQuery();
				customerQuery = gson.fromJson(str, CustomerQuery.class);
			}
		}
		return customerQuery;
	}

	/**
	 * 添加用户归属
	 * 
	 * @param ca
	 * @return
	 * @throws Exception
	 */
	public boolean AttributionCreate(CustomerAttribution customerAttribution)
			throws Exception {
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(customerAttribution);
		strPost = "{\"model\":" + strPost + "}";
		String strReturn = HttpHelper.getInstance(_context).HttpPost(
				ComConst.SERVICE_URL, "AttributionCreate", strPost);
		if (ValidateResult(strReturn)) {
			// {"CustomerCreateResult":false}
			strReturn = strReturn.split(":")[1];
			strReturn = strReturn.substring(0, strReturn.length() - 1);
		}
		return Boolean.valueOf(strReturn);
	}

	/**
	 * 根据当前登录用户下的其中一个部门员工来查询客户
	 * 
	 * @param examineQuery
	 * @return
	 * @throws Exception
	 */
	public List<CustomerDetail> CustomerExamineQuery(ExamineQuery examineQuery)
			throws Exception {
		List<CustomerDetail> list = null;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(examineQuery);
		strPost = "{\"model\":" + strPost + "}";
		String strReturn = HttpHelper.getInstance(_context).HttpPost(
				ComConst.SERVICE_URL, "CustomerExamineQuery", strPost);
		if (ValidateResult(strReturn)) {
			List<String> lstRe = ProcessResult(strReturn,
					"CustomerExamineQueryResult");
			for (String str : lstRe) {
				list = new ArrayList<CustomerDetail>();
				JSONArray ja = new JSONArray(str);
				for (int i = 0; i < ja.length(); i++) {
					CustomerDetail customerDetail = gson.fromJson(
							ja.getString(i), CustomerDetail.class);
					list.add(customerDetail);
				}
			}
		}
		return list;
	}

	/**
	 * 归属查询
	 * 
	 * @param examineQuery
	 * @return
	 * @throws Exception
	 */
	public List<CustomerAttribution> AttributionExamineQuery(
			ExamineQuery examineQuery) throws Exception {
		List<CustomerAttribution> list = null;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(examineQuery);
		strPost = "{\"model\":" + strPost + "}";
		String strReturn = HttpHelper.getInstance(_context).HttpPost(
				ComConst.SERVICE_URL, "AttributionExamineQuery", strPost);
		if (ValidateResult(strReturn)) {
			List<String> lstRe = ProcessResult(strReturn,
					"AttributionExamineQueryResult");
			for (String str : lstRe) {
				list = new ArrayList<CustomerAttribution>();
				JSONArray ja = new JSONArray(str);
				for (int i = 0; i < ja.length(); i++) {
					CustomerAttribution customerAttribution = gson.fromJson(
							ja.getString(i), CustomerAttribution.class);
					list.add(customerAttribution);
				}
			}
		}
		return list;
	}

	/**
	 * 沟通（归属）记录查询
	 * 
	 * @param examineQuery
	 * @return
	 * @throws Exception
	 */
	public List<CustomerAttribution> AttributionQueryByUserNum(
			AttributionQueryParam param) throws Exception {
		List<CustomerAttribution> list = null;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(param);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(
				ComConst.SERVICE_URL, "AttributionList", strPost);
		if (ValidateResult(strReturn)) {
			List<String> lstRe = ProcessResult(strReturn,
					"AttributionListResult");
			for (String str : lstRe) {
				list = new ArrayList<CustomerAttribution>();
				JSONArray ja = new JSONArray(str);
				for (int i = 0; i < ja.length(); i++) {
					CustomerAttribution customerAttribution = gson.fromJson(
							ja.getString(i), CustomerAttribution.class);
					list.add(customerAttribution);
				}
			}
		}
		return list;
	}

	/**
	 * 沟通（归属）记录查询
	 * 
	 * @param examineQuery
	 * @return
	 * @throws Exception
	 */
	public List<Attribution> AttributionOfCustomer(int customerID)
			throws Exception {
		List<Attribution> list = null;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = "{\"customerID\":\"" + customerID + "\"}";
		String strReturn = HttpHelper.getInstance(_context).HttpPost(
				ComConst.SERVICE_URL, "AttributionOfCustomer", strPost);
		if (ValidateResult(strReturn)) {
			List<String> lstRe = ProcessResult(strReturn,
					"AttributionOfCustomerResult");
			for (String str : lstRe) {
				list = new ArrayList<Attribution>();
				JSONArray ja = new JSONArray(str);
				for (int i = 0; i < ja.length(); i++) {
					Attribution attribution = gson.fromJson(ja.getString(i),
							Attribution.class);
					list.add(attribution);
				}
			}
		}
		return list;
	}
	/**
	 * 客户审核
	 * 
	 * @param customerExamine
	 * @return
	 * @throws Exception
	 */
	public boolean CustomerAudit(CustomerExamine customerExamine)
			throws Exception {
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(customerExamine);
		strPost = "{\"model\":" + strPost + "}";
		String strReturn = HttpHelper.getInstance(_context).HttpPost(
				ComConst.SERVICE_URL, "CustomerExamine", strPost);
		if (ValidateResult(strReturn)) {
			// {"CustomerCreateResult":false}
			strReturn = strReturn.split(":")[1];
			strReturn = strReturn.substring(0, strReturn.length() - 1);
		}
		if (strReturn.equals("true")) {
			return Boolean.valueOf(strReturn);
		} else {
			return false;
		}
	}

	/**
	 * 归属记录审核
	 * 
	 * @param attributionExamine
	 * @return
	 * @throws Exception
	 */
	public boolean RecordAudit(AttributionExamine attributionExamine)
			throws Exception {
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(attributionExamine);
		strPost = "{\"model\":" + strPost + "}";
		String strReturn = HttpHelper.getInstance(_context).HttpPost(
				ComConst.SERVICE_URL, "AttributionExamine", strPost);
		if (ValidateResult(strReturn)) {
			// {"CustomerCreateResult":false}
			strReturn = strReturn.split(":")[1];
			strReturn = strReturn.substring(0, strReturn.length() - 1);
		}
		if (strReturn.equals("true")) {
			return Boolean.valueOf(strReturn);
		} else {
			return false;
		}
	}

	/**
	 * 添加短信记录
	 * 
	 * @param smsLog
	 * @return
	 * @throws Exception
	 */
	public int SMSLogCreate(SMSLog smsLog) throws Exception {
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(smsLog);
		strPost = String.format("{\"list\":[%1$s]}", strPost);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(
				ComConst.SERVICE_URL, "SMSLogCreate", strPost);
		if (ValidateResult(strReturn)) {
			List<String> lstRe = ProcessResult(strReturn, "SMSLogCreateResult");
			for (String str : lstRe) {
				if (null != str && "1".equals(str)) {
					return 1;
				}
			}
		}
		return -1;
	}

	/**
	 * 手机通话记录添加
	 * 
	 * @param callLog
	 * @return
	 * @throws Exception
	 */
	public int CallLogCreate(CallLog callLog) {
		try {
			GsonBuilder gsonb = new GsonBuilder();
			DateDeserializer dds = new DateDeserializer();
			gsonb.registerTypeAdapter(Date.class, dds);
			Gson gson = gsonb.create();
			String strPost = gson.toJson(callLog);
			strPost = String.format("{\"list\":[%1$s]}", strPost);
			String strReturn = HttpHelper.getInstance(_context).HttpPost(
					ComConst.SERVICE_URL, "CallLogCreate", strPost);
			if (ValidateResult(strReturn)) {
				List<String> lstRe = ProcessResult(strReturn,
						"CallLogCreateResult");
				for (String str : lstRe) {
					if (null != str && "1".equals(str)) {
						return 1;
					}
				}
			}
		} catch (Exception e) {
			Log.e("CallLogCreate", ComFunc.getExceptionMessage(e), e);
		}
		return -1;
	}

	/**
	 * 指定客户的通话记录
	 * 
	 * @param customerID
	 * @return
	 * @throws Exception
	 */
	public List<CallLog> CallLogList(int customerID) throws Exception {
		List<CallLog> list = null;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(customerID);
		strPost = "{\"customerID\":\"" + customerID + "\"}";
		String strReturn = HttpHelper.getInstance(_context).HttpPost(
				ComConst.SERVICE_URL, "CallLogList", strPost);
		if (ValidateResult(strReturn)) {
			List<String> lstRe = ProcessResult(strReturn, "CallLogListResult");
			for (String str : lstRe) {
				list = new ArrayList<CallLog>();
				JSONArray ja = new JSONArray(str);
				for (int i = 0; i < ja.length(); i++) {
					CallLog callLog = gson.fromJson(ja.getString(i),
							CallLog.class);
					list.add(callLog);
				}
			}
		}
		return list;
	}

	/**
	 * 指定客户的通话记录
	 * 
	 * @param customerID
	 * @return
	 * @throws Exception
	 */
	public List<SMSLog> SMSLogList(int customerID) throws Exception {
		List<SMSLog> list = null;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(customerID);
		strPost = "{\"customerID\":\"" + customerID + "\"}";
		String strReturn = HttpHelper.getInstance(_context).HttpPost(
				ComConst.SERVICE_URL, "SMSLogList", strPost);
		if (ValidateResult(strReturn)) {
			List<String> lstRe = ProcessResult(strReturn, "SMSLogListResult");
			for (String str : lstRe) {
				list = new ArrayList<SMSLog>();
				JSONArray ja = new JSONArray(str);
				for (int i = 0; i < ja.length(); i++) {
					SMSLog smsLog = gson
							.fromJson(ja.getString(i), SMSLog.class);
					list.add(smsLog);
				}
			}
		}
		return list;
	}
}
